#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>

template<class T>
struct ListNode
{
	typedef ListNode<T> Node;
	Node* _prev;
	Node* _next;
	T _val;

	ListNode(const T& t = T())
		:_val(t),
		_prev(nullptr),
		_next(nullptr)
	{}
};

template<class T>
class ListIterator
{
	typedef ListNode<T> Node;
public:
	typedef ListIterator<T> Self;
	ListIterator(Node* node)
		:_node(node)
	{}
	//++it
	Self& operator++()
	{
		_node = _node->_next;
		return *this;
	}
	//--it
	Self& operator--()
	{
		_node = _node->_prev;
		return *this;
	}
	//it++
	Self operator++(int)
	{
		Self tmp(*this);
		_node = _node->_next;
		return tmp;
	}
	//it--
	Self operator--(int)
	{
		Self tmp(*this);
		_node = _node->_prev;
		return tmp;
	}

	T& operator*()
	{
		return _node->_val;
	}

	bool operator==(const Self& s)
	{
		return _node == s._node;
	}

	bool operator!=(const Self& s)
	{
		return _node != s._node;
	}
	Node* _node;
};

template<class T>
class List
{
	typedef ListNode<T> Node;
public:
	typedef ListIterator<T> iterator;
	List()
		:_head(new Node)
	{
		_head->_next = _head;
		_head->_prev = _head;
	}

	iterator begin()
	{
		return iterator(_head->_next);
	}

	iterator end()
	{
		return iterator(_head);
	}

	void insert(iterator pos, const T& t)
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* newnode = new Node(t);

		//prev newnode cur
		prev->_next = newnode;
		newnode->_prev = prev;
		newnode->_next = cur;
		cur->_prev = newnode;
	}

	iterator erase(iterator pos)
	{
		Node* cur = pos._node;
		Node* prev = cur->_prev;
		Node* next = cur->_next;

		prev->_next = next;
		next->_prev = prev;
		delete cur;
		return iterator(next);
	}

	void push_back(const T& t)
	{
		insert(end(), t);
	}

	void push_front(const T& t)
	{
		insert(begin(), t);
	}

	void pop_front()
	{
		erase(begin());
	}

	void pop_back()
	{
		erase(--end());
	}

	void clear()
	{
		iterator it = begin();
		while (it != end())
		{
			it = erase(it);
		}
	}


	~List()
	{
		clear();
		delete _head;
		_head = nullptr;
	}

private:
	Node* _head;
};





